<?php
include '../config.php';
header("Content-type=text/html;charset:utf-8");
define('REFERER',isset($_SERVER['HTTP_REFERERR'])?$_SERVER['HTTP_REFERERR']:'');
if(!gCookie('username')){showmessage2('非法操作!');}
$action=$_GET['action'];
$contentid=$_GET['contentid'];
$_userid=gCookie('userid');
$lastMTime=$_userid.'collect';
include '../cms/include/db_mysql.class.php';
include '../class/Cache.php';
$db = new db_mysql();
$cache=new Cache();
$db->connect(DB_HOST, DB_USER, DB_PW, DB_NAME, DB_PCONNECT, DB_CHARSET);
switch($action)
{
	case 'add':
		$contentid = intval($contentid);
		if(!$contentid) showmessage2('缺少信息id，请重新选择要收藏的信息！',REFERER);
		require_once('../cms/include/admin/content.class.php');
		$c = new content();
		$r = $c->get($contentid,1);//var_dump(HTTP_REFERER);exit;		
		$c = $db->get_one("SELECT id FROM ".DB_PRE."collect WHERE `contentid`='$contentid' AND `userid`='$_userid'");
		if($c['id']) showmessage2('此信息您已经收藏！',REFERER);
		$info = array('contentid'=>$contentid, 'userid'=>$_userid, 'addtime'=>time());
		$db->insert(DB_PRE.'collect', $info);
		$cache->store($lastMTime,time());//记录最新的收录时间，为是否更新缓存提供依据
		if($db->insert_id()) showmessage2('收藏成功！',REFERER);
	break;

	case 'delete':
		$id = intval($id);
		if(!$id) showmessage2('非法操作！');
		$db->query("DELETE FROM ".DB_PRE."collect WHERE `userid`='$_userid' AND `id`='$id'");
		showmessage2('删除成功！');
	break;

	default:
		$page=isset($_GET['page'])?(($_GET['page']<1)?1:$_GET['page']):1;
		$limit='limit '.($page-1).' 25';
		$sql='select a.*,b.* from '.DB_PRE.'collect a,'.DB_PRE.'content b where  a.contentid=b.contentid and a.userid='.$_userid;
		if($cache->cache_time($sql)<$cache->cache_time($lastMTime))
		//判断缓存时间与最新收藏时间的大小，小于则更新缓存，否则读取缓存
		{
			$data=$db->select($sql);//var_dump($data['collect']);
			$cache->store($sql,$data);
		}
		else
		{
			$data=$cache->fetch($sql);
		}
		$categoryName='<a href="collect.php">我的收藏</a>';
		include template('phpcms', 'list_news');
	break;
}
?>